<template>
  <div class="login-phone">
    <div class="logo">
      <div class="img-box">
        <img src="@/assets/images/icon_logo .png" alt />
      </div>
      <p>欢迎来到DDS·C设计师俱乐部</p>
    </div>
    <div class="input-wrapper">
      <van-form @submit="onSubmit">
        <van-field
          type="tel"
          placeholder="请输入手机号"
          name="validator"
          :rules="[{ validator, message: '请输入正确手机号码', required: true }]"
          v-model="userInfo.phone"
        >
          <template slot="left-icon">
            <i class="left-icon-phone"></i>
          </template>
        </van-field>
        <van-field
          type="number"
          placeholder="请输入验证码"
          v-model="userInfo.captcha"
          name="pattern"
          :rules="[{ pattern, message: '请输入有效验证码', required: true }]"
        >
          <template slot="left-icon">
            <i class="left-icon-password"></i>
          </template>
          <template slot="extra">
            <span @click="getCodeClick" v-if="showGetCode">{{ codeTxt }}</span>
            <span v-else style="position:relative; left: -0.4rem">{{time}}s</span>
          </template>
        </van-field>
      </van-form>
    </div>
    <div class="btn">
      <my-button name="登 录" @btnClick="onSubmit" />
    </div>
  </div>
</template>
<script>
import { getCaptcha } from '@/api/my'
import wx from 'weixin-js-sdk'
// import { getToken } from '@/util/auth'
const constantTime = 60
const ERR_OK = 200
export default {
  data () {
    return {
      showGetCode: true,
      time: constantTime,
      timer: null,
      codeTxt: '获取验证码',
      userInfo: {
        phone: '',
        captcha: ''
      },
      pattern: /^[0-9]*$/
    }
  },
  watch: {
    time (newVal) {
      if (newVal <= 0) {
        this.timer && clearInterval(this.timer)
        this.showGetCode = true
        this.codeTxt = '重新获取'
        this.time = constantTime
      }
    }
  },
  destroyed () {
    this.timer && clearInterval(this.timer)
  },
  directives: {
    focus: {
      // 指令的定义
      inserted: function (el) {
        el.focus()
      }
    }
  },
  methods: {
    onSubmit () {
      if (localStorage.getItem('GET_SHARE_INTEGRAL')) {
        const obj = JSON.parse(localStorage.getItem('GET_SHARE_INTEGRAL'))
        this.userInfo.inviter = parseInt(obj.id)
        // alert(obj.id)
      }
      const me = this
      this.$store
        .dispatch('captchaLogin', this.userInfo)
        .then(response => {
          if (response.code === ERR_OK) {
            const { data } = response
            if (!this.$route.query.redirect_url) {
              wx.miniProgram.getEnv(ress => {
                if (ress.miniprogram) {
                  wx.miniProgram.navigateTo({
                    url: `/pages/index/index?return_url=https://ddsc.izhixiu.cn/front/#/home&token=${data.token}`
                  })
                }
              })
            } else {
              const baseUrl = me.$route.query.redirect_url
              wx.miniProgram.getEnv(ress => {
                if (ress.miniprogram) {
                  wx.miniProgram.navigateTo({
                    url: `/pages/index/index?return_url=${encodeURIComponent(
                      `https://ddsc.izhixiu.cn/front/#${baseUrl}`
                    )}&token=${data.token}`
                  })
                }
              })
            }
            // this.$router.replace('/home')
          } else {
            this.$toast('手机号或验证码错误')
          }
        })
        .catch(() => {})
    },
    validator (val) {
      return /^[1][3,4,5,7,8,9][0-9]{9}$/.test(val)
    },
    getCodeClick () {
      if (this.showGetCode) {
        if (!this.userInfo.phone) {
          this.$toast('手机号码不能为空')
          return
        }
        this._getCaptcha(this.userInfo.phone).then(() => {
          this.showGetCode = false
          this.timer = setInterval(() => {
            this.time -= 1
          }, 1000)
        })
      }
    },
    _getCaptcha (phone) {
      return getCaptcha(phone)
        .then(res => {
          if (res.code === ERR_OK) {
            return Promise.resolve(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.login-phone {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background: #fff;
  z-index: 8;
  padding: 0.96rem 1.12rem 0.92rem 1.12rem;
  box-sizing: border-box;
  .logo {
    .img-box {
      width: 2.26rem;
      height: 0.71rem;
      img {
        width: 100%;
        vertical-align: top;
      }
    }
    p {
      font-size: 0.34rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: rgba(153, 153, 153, 1);
      line-height: 0.35rem;
      margin-top: 0.32rem;
    }
  }
  .input-wrapper {
    margin-top: 1.14rem;
    .left-icon-phone {
      display: inline-block;
      width: 0.26rem;
      height: 0.39rem;
      background: url("../../assets/images/icon_sjh.png") no-repeat;
      background-size: cover;
    }
    .left-icon-password {
      display: inline-block;
      width: 0.28rem;
      height: 0.38rem;
      background: url("../../assets/images/icon_yzm.png") no-repeat;
      background-size: cover;
    }
    .van-cell {
      padding-left: 0;
      padding-right: 0;
      // padding: 15px 0;
      padding: 0 !important;
      border-bottom: 1px solid #eee;
      display: flex;
      align-items: center;
      /deep/.van-field__body {
        margin-left: 0.3rem;
        input {
          padding: 0.35rem 0;
        }
      }
      /deep/ .van-field__left-icon {
        display: flex;
        flex-direction: column;
        justify-content: center;
        i {
          vertical-align: middle;
        }
      }
      &::after {
        left: 0;
        border-bottom: none;
      }
    }
  }
  .btn {
    margin-top: 4.32rem;
  }
}
</style>
